package mlProject;

import java.util.ArrayList;

import tool.ComputeIDF;

/**
 * 
 * @author zhangjinrui
 * 这个类用来表示文档模型
 */
public class DocModel {
	
	public ArrayList<Feature> features;
	public String label;
	public String id;
	public int sum;//单词总数
	public double max;
	
	public DocModel(){
		this.features = new ArrayList<DocModel.Feature>();
		this.sum=0;
	}
	
	public void addFeature(Feature f){
		this.features.add(f);
	}
	
	public static class Feature{
		public String name;
		public double value;
		
		public Feature(String name, double value){
			this.name = name;
			this.value = value;
		}
		
		public String toString(){
			return name + ": " + value;
		}
	}
	
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append("ID: ");
		sb.append(id);
		sb.append("\n");
		sb.append("  label: ");
		sb.append(label);
		sb.append("\n");
		for(int i = 0 ; i < features.size() ; i++){
			sb.append("\t");
			sb.append(features.get(i));
			sb.append("\n");
		}
		sb.append("\n");
		return sb.toString();
	}
	public void toTFIDF(){
		
		for(int i=0;i<features.size();i++){
			String name=features.get(i).name;
			Double idf_temp=0.0;
			if(ComputeIDF.idf.containsKey(name)) idf_temp=ComputeIDF.idf.get(name);
			else idf_temp=Math.log(0.01);
			features.get(i).value=(features.get(i).value/max)*idf_temp;
		}
	}
	
}


